package com.bluemsun.naturallanguageprocessing.controller;

import com.bluemsun.naturallanguageprocessing.common.ServerResponse;
import com.bluemsun.naturallanguageprocessing.entity.UserApi;
import com.bluemsun.naturallanguageprocessing.service.IGoodService;
import com.bluemsun.naturallanguageprocessing.util.ConductRedisUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.Min;

/**
 * @author :hucl
 * @describe :用户点赞
 */
@Validated
@RestController
@RequestMapping("/good")
public class GoodController {

    @Resource
    private IGoodService goodService;

    @Resource
    private ConductRedisUtil conductRedisUtil;

    /**
     * 用户点赞
     * @param request
     * @param questionId
     * @return
     */
    @GetMapping("/do/{questionId}")
    public ServerResponse userDoGood(HttpServletRequest request, @Min(1) @PathVariable("questionId") int questionId){
        //TODO: 后期打开redis
        UserApi user = conductRedisUtil.getUserFromRedis(request.getHeader("token"));
        int num = goodService.userDoGood(user.getId(), questionId);
        return ServerResponse.createBySuccess("点赞成功");
    }
    /**
     * 用户取消点赞
     * @param request
     * @param questionId
     * @return
     */
    @GetMapping("/undo/{questionId}")
    public ServerResponse userUnDoGood(HttpServletRequest request, @Min(1) @PathVariable("questionId") int questionId){
        //TODO: 后期打开redis
        UserApi user = conductRedisUtil.getUserFromRedis(request.getHeader("token"));
        int num = goodService.userUnDoGood(user.getId(), questionId);
        return ServerResponse.createBySuccess("取消点赞成功");
    }
}
